- main_toolbar do |t|
  = t.edit resource
  = t.new_inventory params: { controller: :inventories, action: :new, product_nature_variant_id: resource.id }, icon: false, label: :new_inventory.tl

= main_informations attachment: true do
  = infos do
    = resource_info(:number, important: true)
    = resource_info(:work_number, important: true)
    .med-info.important
      - icon = resource.category.pictogram
      %span.title= ProductNatureVariant.human_attribute_name(:nature)
      %span.value
        %i.picto{class: "picto-#{icon}"}
        = link_to(resource.nature_name, controller: :product_natures, action: :show, id: resource.nature_id)
    - if resource.gtin?
      .med-info.important
        %span.title= :gtin.tl
        %span.value
          %i.picto{class: "picto-barcode"}
          = resource.gtin
    - if resource.current_stock
      .med-info.important
        %span.title= :population.tl
        %span.value
          %i.picto{class: "picto-stock"}
          = resource.current_stock.to_s + " " + resource.unit_name
    - if resource.france_maaid?
      = resource_info(:france_maaid) do |france_maaid|
        %i.picto{class: "picto-flask"}
        = link_to(france_maaid, "https://ephy.anses.fr/resultats_recherche/ppp?search_api_aggregation_3=#{france_maaid}")
  -# .info
    %span.title= ProductNatureVariant.human_attribute_name(:net_mass)
    %span.value= resource.products.map(&:net_mass).compact.sum
  -# .info
    %span.title= ProductNatureVariant.human_attribute_name(:net_volume)
    %span.value= resource.products.map(&:net_volume).compact.sum
  -# if resource.picture.exists?
    = image_tag(resource.picture.url(:thumb))

  = attributes_list(resource) do |l|
    - l.attribute :name
    - l.attribute :unit_name
    - l.attribute :variety
    - l.attribute :derivative_of
    - l.attribute :stock_account, url: true
    - l.attribute :stock_movement_account, url: true


= cobbles do |c|
  - if resource.france_maaid? && (agent = Pesticide::Agent.find(resource.france_maaid))
    - c.cobble(:phyto_data) do
      - if agent.risks.any?
        - avatars = []
        - list = agent.risks.map do |risk|
          - classification = Nomen::ChemicalClassification.find_by(code: risk)
          - if classification
            - path = item_avatar_path(classification)
            - avatars << classification if path && !avatars.include?(classification)
            - content_tag(:abbr, risk, title: classification.human_name)
          - else
            - content_tag(:span, risk)
        .med-info
          .value
            - if avatars.any?
              - avatars.each do |item|
                = image_tag(item_avatar_path(item), class: 'risk-picto', title: item.human_name, alt: item.code)
            = list.to_sentence.html_safe
      - if agent.usages.any?
        %table.table
          %thead
            %tr
              %th{colspan: 3} Usage
              %th Dose
              %th= Pesticide::Usage.human_attribute_name(:max_inputs_count)
              %th= Pesticide::Usage.human_attribute_name(:pre_harvest_interval)
              %th= Pesticide::Usage.human_attribute_name(:untreated_zone_margin)
          %tbody
            - agent.usages.each do |usage|
              %tr{class: (usage.subject_variety.blank? ? '' : Activity.of_cultivation_variety(usage.subject_variety).any? ? '' : 'disabled') }
                %td
                  - if usage.subject_variety
                    = Nomen::Variety.find(usage.subject_variety).human_name
                  - else
                    = usage.subject_name
                %td= usage.mode
                %td= usage.issue
                %td= usage.dose.l if usage.dose
                %td= usage.max_inputs_count
                %td= usage.pre_harvest_interval.l if usage.pre_harvest_interval
                %td= usage.untreated_zone_margin.l if usage.untreated_zone_margin

  -# if resource.products.any?
    - c.cobble(:stocks) do
      :ruby
        dataset = resource.products

        series = []
        variant_data = {}
        dataset.each do |product|
          items = ProductMovement.where(product: product).reorder(:started_at)
          item_h = items.sort.inject({}) do |hash, pair|
            hash[pair.started_at.to_usec] = pair.population.to_d
            hash
          end
          data = []
          data = item_h.collect{|k,v| [k, v.to_s.to_f] }.sort_by(&:first)
          series << {name: product.name, data: data }
        end
        datas = series.map { |s| s[:data] }
        sorted_datas = datas.map do |data|
          changes = (data + [[0, 0]]).sort_by(&:first).reverse.each_cons(2).map do |one, two|
            [one.first, one.last - two.last]
          end
          changes.reverse
        end
        variant_datas = sorted_datas.flatten(1).sort_by(&:first).reduce([]) do |vd, data_point|
          key = data_point.first
          value = Maybe(vd.last).last.or_else(0)
          vd << [key, value+data_point.last]
        end
        series << {name: "Variant " + resource.name, data: variant_datas.to_a}

      = line_highcharts(series, legend: { layout: 'vertical', align: 'bottom', vertical_align: 'bottom', border_width: 0 }, y_axis: {title: { text: :population.tl}, min: 0}, x_axis: {type: 'datetime', title: { enabled: true, text: :months.tl}})

  - c.cobble(:products) do
    = cobble_list(:products)

  - c.cobble(:sale_items) do
    = cobble_list(:sale_items)

  - c.cobble(:parcel_items) do
    = cobble_list(:parcel_items)

  - c.cobble(:catalog_items) do
    = cobble_toolbar do |t|
      = t.new(params: {controller: :catalog_items, :variant_id => resource.id, redirect: request.fullpath}, tool: 'new-price')
    = cobble_list(:catalog_items)

  - if resource.of_variety? :equipment
    - c.cobble(:components) do
      = cobble_list(:components)

